/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.datetime.RecurrencePattern;
import org.openaecfm.model.datetime.SchedulingTime;
import org.openaecfm.model.datetime.WorkTime;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class WorkTimeIO extends AbstractEntityInstanceInputImpl<WorkTime>
		implements IEntityInstanceOutput<WorkTime> {

	private IEntityInstanceUtilOutput<SchedulingTime> utilOutput;
	private IEntityInstanceUtilInput<SchedulingTime> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SchedulingTime> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<SchedulingTime> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(WorkTime pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCWorkTime",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set RecurrencePattern
		if (pojo.getRecurrencePattern() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getRecurrencePattern())));
		}

		// Set Start
		if (pojo.getStart() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getStart().toString(),
					StringParameter.UTF_8));
		}

		// Set Finish
		if (pojo.getFinish() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getFinish().toString(),
					StringParameter.UTF_8));
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, WorkTime pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RecurrencePattern
		Parameter rp = sei.getParameter(3);
		if (rp.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) rp;
			pojo.setRecurrencePattern((RecurrencePattern) appDataManager
					.getObject(e.toInstanceName()));
		}
	}

	@Override
	protected WorkTime doCreateObject() {
		return new WorkTime();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, WorkTime pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Start
		Parameter start = sei.getParameter(4);
		if (start.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) start;
			pojo.setStart(new Date(s.toUTF8()));
		}

		// Set Finish
		Parameter finish = sei.getParameter(5);
		if (finish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) finish;
			pojo.setFinish(new Date(s.toUTF8()));
		}
	}
}
